﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Datawin;

namespace MapControlSample
{
    public partial class Page : UserControl
    {
        private Datawin.Country USData;
        
        public Page()
        {
            InitializeComponent();
            InitPage();
        }

        private void InitPage()
        {
            
            Color colorBlue = new Color();
            colorBlue.B = 50;

            
            Color colorGrey = new Color();
            colorGrey.B = 70;
            colorGrey.G = 70;
            colorGrey.R = 70;
            colorGrey.A = 255;

            Color colorPaleYellow = new Color();
            colorPaleYellow.B = 132;
            colorPaleYellow.G = 231;
            colorPaleYellow.R = 239;
            colorPaleYellow.A = 255;


            //Instantiate Map Control and add it to our canvas
            USMapControl map = new USMapControl();
            map.NumberFormat = "#,#";
            map.CurrencySymbol = "$";
            LayoutRoot.Children.Add(map);
            Canvas.SetLeft(map, 100);
            Canvas.SetTop(map, -20);

            //Assign the properties of the Map Control
            map.States = GetStates();
            map.HeatColor = colorBlue;
            map.RollOverColor = colorPaleYellow;
            map.Update();

            
            map.StateClickCallback = ClickCallback;
            ScaleTransform scaleTransform = new ScaleTransform();
            scaleTransform.ScaleX = .5;
            scaleTransform.ScaleY = .5;
            map.RenderTransform = scaleTransform;
        }

        public void ClickCallback(Path pathState, State state)
        {
            //string diplayText = "{0} - {1}";
            //string displayText2 = "Total Sales:{0}  Total Units:{1}";
            //txtData.Text = String.Format(diplayText,state.Name,state.Value) ;

            var results = (from s in USData.States
                           where s.Name == state.Name
                           select s);
            Datawin.State dataState = results.First();
            ProductsGrid.ItemsSource =  dataState.Products;

            //txtData.Text = String.Format(displayText2, dataState.Products[0].Units, dataState.TotalUnits);




        }


        //Get a list of states and assigns them random values
        //The "State" class is defined in the USMapControl
        private List<State> GetStates()
        {
            USData = Datawin.Country.GetUSAWithRandomData();
            List<State> StateList = new List<State>();

            foreach (Datawin.State state in USData.States)
            {
                StateList.Add(new State(state.Name, state.TotalSales));

            }
            return StateList;
            //int lowerValueOfRand = 12000;
            //int upperValueOfRand = 54000;

            //List<State> StateList = new List<State>();
            //string states = "AL,AK,AZ,AR,CA,CO,CT,DE,FL,GA,HI,ID,IL,IN,IA,KS,KY,LA,ME,MD,MA,MI,MN,MS,MO,MT,NE,NV,NH,NJ,NM,NY,NC,ND,OH,OK,OR,PA,RI,SC,SD,TN,TX,UT,VT,VA,WA,WV,WI,WY";
            //string[] statesArr = states.Split(',');

            //Random random = new Random();
            //for (int i = 0; i <= statesArr.GetUpperBound(0); i++)
            //{
            //    double stateValue = random.Next(lowerValueOfRand, upperValueOfRand);
            //    stateValue = stateValue / 100;
            //    State thisState = new State(statesArr[i], stateValue);
            //    StateList.Add(thisState);

            //}
            //return StateList;
        }

       
    }





}
